//
//  PercentCurrentPiePlot.m
//  Balance
//
//  Created by oracle on 7/20/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "PercentCurrentPiePlot.h"

@implementation PercentCurrentPiePlot


@synthesize graph,pieData,item;


- (id) initWithMetricsData:(MetricsItem *) data {
    
    item=data;    
    return self;
    
}

- (void)respondToValueChange: (PercentCurrentLinePlot *)plot item:( MetricsItem *) metricsItem{
    
    item=metricsItem;

	self.pieData=  [NSMutableArray arrayWithObjects:
                    item.percentCurrent, 
                    item.percent1To30DaysDelinquent,
                    item.percent31To60DaysDelinquent,
                    item.percent61To90DaysDelinquent,
                    item.percent90OverDaysDelinquent, 
                    nil];
    [graph reloadData];
}

- (void)renderInLayer:(CPTGraphHostingView *)layerHostingView withTheme:(CPTTheme *)mytheme{
  
    CGRect bounds = layerHostingView.bounds;
    
    graph = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
    
    layerHostingView.hostedGraph = graph;
    CPTTheme *theme = [CPTTheme themeNamed:kCPTSlateTheme];    
   //[graph applyTheme:theme];
    
    layerHostingView.backgroundColor = [UIColor colorWithWhite:0.7f alpha:1.0f]; 
    
    CPTGradient *plotAreaGradient = [CPTGradient gradientWithBeginningColor:
                                    [CPTColor colorWithGenericGray:0.9f]
                                    endingColor:[CPTColor colorWithGenericGray:0.7f]];
    plotAreaGradient.angle = -90.0;
    graph.plotAreaFrame.plotArea.fill = [CPTFill fillWithGradient:plotAreaGradient]; 
    
       
    graph.plotAreaFrame.masksToBorder = NO;
    
    CPTMutableTextStyle *blackTextStyle = [[CPTMutableTextStyle alloc] init];
   
    blackTextStyle.color = [CPTColor colorWithComponentRed:0.2 green:0.2 blue:0.2 alpha:0.82]; //RGB:R51-G51-B51
	blackTextStyle.fontSize = 20.0;
    graph.titleTextStyle = blackTextStyle;
     graph.titleDisplacement = CGPointMake(0.0, 20.0); 
    graph.title = @"Percent Delinquent As of ";
    
    // Add plot space for horizontal bar charts
    graph.paddingLeft = 90.0;
	graph.paddingTop = 50.0;
	graph.paddingRight = 70.0;
	graph.paddingBottom = 1.0; 
    graph.axisSet=nil;
        
    // Add pie chart
    CPTPieChart *piePlot = [[CPTPieChart alloc] init];    
        
	piePlot.dataSource = self;
	piePlot.pieRadius = 85.0;
	piePlot.identifier = @"PercentCurrentPieChart";
	piePlot.startAngle = M_PI_4;
	piePlot.sliceDirection = CPTPieDirectionCounterClockwise;
    //piePlot.centerAnchor = CGPointMake(0.5, 0.38);
    //piePlot.borderLineStyle = [CPTLineStyle lineStyle];     
    
	self.pieData=  [NSMutableArray arrayWithObjects:
                   item.percentCurrent, 
                   item.percent1To30DaysDelinquent,
                   item.percent31To60DaysDelinquent,
                   item.percent61To90DaysDelinquent,
                   item.percent90OverDaysDelinquent, 
                   nil];
    
    
    [graph addPlot:piePlot];
    }
-(CGFloat)radialOffsetForPieChart:(CPTPieChart *)pieChart recordIndex:(NSUInteger)index
{
    return ( index == 0 ? 0.0f : 0.0f );
}

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
    return [self.pieData count];
}

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index 
{
    if ( index >= [self.pieData count] ) return nil;
    
    if ( fieldEnum == CPTPieChartFieldSliceWidth ) {
        return [self.pieData objectAtIndex:index];
    }
    else {
        return [NSNumber numberWithInt:index];
    }	
}

- (void)viewDidUnload  {
	[self setPieData:nil];
	[graph setGraph:nil];	
}


@end
